import re
from pathlib import Path



class ParseConfig:
    def __init__(self, config_file, vendor, model):
        self.config_file = config_file
        self.config = {}
        self.model = model
        self.vendor = vendor
        self.vendor_info = {
            'huawei': {                   
                'S9303': {
                        'config_start': '!Software',                        
                        'config_end': 'end',
                        'vrp_version': 'V200R019C10SPC500'
                },
            },
            'maipu': {
                    'S4330': {
                        'config_start': '!',                        
                        'config_end': '!end',                        
                        'vrp_version': '9.5.0.3'
                    }
            }
        }

    # 获取vendor信息
    def get_vendor_info(self):
        # 如果获取不到vendor信息，则返回空字典
        if self.vendor not in self.vendor_info:
            return {"error": "vendor not found"}
        else:
            return self.vendor_info[self.vendor]

    # 获取model信息
    def get_model_info(self, vendor):
        # 如果获取不到model信息，则返回空字典
        if vendor not in self.vendor_info:            
            return {"error": "vendor not found"}
        else:
            return self.vendor_info[vendor]['models']

    # 获取vrp版本号
    def get_vrp_version(self, vendor, model):
        # 如果获取不到vrp版本号，则返回空字符串
        if vendor not in self.vendor_info:
            return {"error": "vendor not found"}
        else:
            models = self.vendor_info[vendor]['models']
            if model not in models:
                return {"error": "model not found"}
            else:
                return models[model]['vrp_version']

    # 是否支持该vendor校验函数
    def _is_support_vendor(self, vendor):
        if vendor in self.vendor_info:
            return True
        else:
            return False

    # 配置文件是否存在
    def _is_config_exist(self):
        file_path = Path(self.config_file)
        if file_path.exists():
            return True
        else:
            return False

    # 截取开头和结尾中间的配置
    def _get_config_content(self, config):
        start_index = config.find('!')
        if start_index!= -1:
            start_index += 1  # 跳过!字符
            end_index = config.find('return', start_index)
            if end_index!= -1:
                result = config[start_index:end_index]
                return result
            else:
                print("没有找到结尾的'return'字符串")
        else:
            print("没有找到开头的'!'字符串")

    # 读取配置文件
    def read_config(self):
        if not self._is_config_exist():
            return {"error": "config file not exist"}
        else:
            with open(self.config_file, 'r') as f:
                config = f.read()
                print(self._get_config_content(config))

    # 解析配置文件
    def _parse_config(self):
        if not self._is_support_vendor(self.vendor):
            return {"error": "vendor not support"}
        else:
            pass

if __name__ == '__main__':
    config_file = r'C:\Users\zhuwei\Desktop\code\parser-network\config\config.txt'
    vendor = 'huawei'
    os_version = 'S9303'
    pc = ParseConfig(config_file, vendor, os_version)
    print(pc.get_vendor_info())
    # print(pc.get_model_info(vendor))
    print(pc.read_config())

